package opengl.pac.tutorial;

import java.io.IOException;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class CameraView extends SurfaceView implements Callback
{
    private Camera _camera;
 
    public CameraView(Context context)
    {
        super(context);
        
        getHolder().addCallback(this);
        
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
 
    public void surfaceCreated( SurfaceHolder holder )
    {
        _camera = Camera.open();
    }
 
    public void surfaceChanged( SurfaceHolder holder, int format, int width, int height )
    {
        Camera.Parameters p = _camera.getParameters();
        
        p.setPreviewSize( width, height );
        
        _camera.setParameters( p );
        
        try
        	{ _camera.setPreviewDisplay(holder); } 
        catch (IOException e)
        	{ e.printStackTrace(); }
        
        _camera.startPreview();
    }
 
    public void surfaceDestroyed( SurfaceHolder holder )
    {
        _camera.stopPreview();
        _camera.release();
        _camera = null;
    }
}